using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.Goods.Dto;
using JTZY.Model.Goods;
using JTZY.Repository;
using JTZY.Service.Goods.IGoodsService;
using JTZY.Common;
using JTZY.Model.Business;

namespace JTZY.Service.Goods
{
    /// <summary>
    /// 代理货品池Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICorecmsagentproductsService), ServiceLifetime = LifeTime.Transient)]
    public class CorecmsagentproductsService : BaseService<Corecmsagentproducts>, ICorecmsagentproductsService
    {
        /// <summary>
        /// 查询本店良品仓
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<ProductStockDto> GetList(CorecmsagentproductsQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable().LeftJoin<Corecmsgoods>((aprod,goods)=>aprod.GoodId==goods.Id)
                .LeftJoin<Corecmsproducts>((aprod,goods,prod)=>aprod.ProductId==prod.Id)
                .WhereIF(!string.IsNullOrEmpty(parm.Sn), (aprod,goods,prod)=>prod.Sn.Contains(parm.Sn) || goods.Name.Contains(parm.Sn) || prod.SkuName.Contains(parm.Sn))
                .Select((aprod, goods, prod)=>new ProductStockDto
                { 
                    Id=aprod.Id,
                    SkuImage = !string.IsNullOrWhiteSpace(aprod.SkuImage)? aprod.SkuImage :goods.Image,
                    Sn=prod.Sn,
                    SkuName=prod.SkuName,
                    Stock=aprod.Stock,
                    MinStock=aprod.MinStock,
                    VIP0=aprod.Vip0,
                    VIP1=aprod.Vip1,
                    VIP2 = aprod.Vip2,
                    VIP3 = aprod.Vip3,
                    VIP4 = aprod.Vip4,
                    BarCode=prod.Barcode,
                    CostPrice=aprod.ProductCostPrice,
                    Atrr="铺货商品",
                    SkuLocation=aprod.SkuLocation,
                    CategoryId=goods.GoodsCategoryId,
                    FirstCategoryId=goods.GoodsFirstCategoryId,
                    SecondCategoryId=goods.GoodsSecondCategoryId,
                    SkuBrandId=aprod.SkubrandId.HasValue? aprod.SkubrandId.Value: goods.BrandId,
                })
                //.OrderBy("CreateTime asc")
                .ToPage<ProductStockDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Corecmsagentproducts GetInfo(long Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加代理货品池
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Corecmsagentproducts AddCorecmsagentproducts(Corecmsagentproducts model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改代理货品池
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCorecmsagentproducts(Corecmsagentproducts model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 上下架
        /// </summary>
        /// <param name="modal"></param>
        /// <returns></returns>
        public int Marketable(Corecmsagentproducts model) 
        {
            return Update(u => u.Id == model.Id, it => new Corecmsagentproducts() { Marketable = model.Marketable, UpdateTime = DateTime.Now });

        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<Corecmsagentproducts> QueryExp(CorecmsagentproductsQueryDto parm)
        {
            var predicate = Expressionable.Create<Corecmsagentproducts>();

            return predicate;
        }
    }
}